/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package propiedades;

import java.util.Properties;
import javax.swing.JOptionPane;
import vista.ClsInicio;

/**
 *
 * @author francisco
 */
public class DialogConfigDatabase extends javax.swing.JDialog {

    /**
     * Creates new form DialogConfigDatabase
     */
    public DialogConfigDatabase(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtClave = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        txtHost = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("CONFIGURACION BASE DE DATOS"));
        jPanel1.setLayout(null);

        jLabel1.setText("   Clave BD:");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 63, 110, 20);
        jPanel1.add(txtUser);
        txtUser.setBounds(110, 30, 160, 30);

        jLabel2.setText("Usuario BD:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 33, 110, 20);
        jPanel1.add(txtClave);
        txtClave.setBounds(110, 60, 160, 30);

        jLabel3.setText("         Host:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(10, 90, 110, 30);
        jPanel1.add(txtHost);
        txtHost.setBounds(110, 90, 160, 30);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 10, 280, 130);

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(160, 150, 100, 30);

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAceptar);
        btnAceptar.setBounds(50, 150, 100, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-315)/2, (screenSize.height-223)/2, 315, 223);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        if (!compruebaVacios()) {
            Propiedades p = new Propiedades();
            Properties prop = p.getProperties("/propiedades/database.properties");
            prop.setProperty("host", txtHost.getText());
            prop.setProperty("user", txtUser.getText());
            prop.setProperty("clave", String.valueOf(txtClave.getPassword()));
            p.setProperties(prop, "/propiedades/database.properties");
            System.out.println("Si llega");
            JOptionPane.showMessageDialog(null, "Por favor reinicie la aplicacion para aplicar los cambios");
            dispose();            
//            ClsInicio.iniciAplicacion();
        } else {
            JOptionPane.showMessageDialog(null, "Debe llenar todos los campos");
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogConfigDatabase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogConfigDatabase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogConfigDatabase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogConfigDatabase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogConfigDatabase dialog = new DialogConfigDatabase(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField txtClave;
    private javax.swing.JTextField txtHost;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables

    private boolean compruebaVacios() {
        if (String.valueOf(txtClave.getPassword()).isEmpty()
                || txtUser.getText().isEmpty() || txtHost.getText().isEmpty()) {
            return true;
        }
        return false;
    }
}
